/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.hk.adv.service;

import java.util.List;

import com.thinkgem.jeesite.modules.hk.adv.entity.HkAllAdv;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.modules.hk.adv.entity.HkCityAdv;
import com.thinkgem.jeesite.modules.hk.adv.dao.HkCityAdvDao;

/**
 * 城市广告管理Service
 * @author 罗天文
 * @version 2018-03-20
 */
@Service
@Transactional(readOnly = true)
public class HkCityAdvService extends CrudService<HkCityAdvDao, HkCityAdv> {
	@Autowired
	HkAllAdvService hkAllAdvService;
	public HkCityAdv get(String id) {
		return super.get(id);
	}
	
	public List<HkCityAdv> findList(HkCityAdv hkCityAdv) {
		return super.findList(hkCityAdv);
	}
	
	public Page<HkCityAdv> findPage(Page<HkCityAdv> page, HkCityAdv hkCityAdv) {
		return super.findPage(page, hkCityAdv);
	}
	
	@Transactional(readOnly = false)
	public void save(HkCityAdv hkCityAdv) {
		hkCityAdv.setCategory(null);
		if(null!=hkCityAdv.getCategorys()){
			hkCityAdv.setCategory(String.join(",", hkCityAdv.getCategorys()));
		}
		hkCityAdv.setTag(null);
		if(null!=hkCityAdv.getTags()){
			hkCityAdv.setTag(String.join(",", hkCityAdv.getTags()));
		}
		super.save(hkCityAdv);

		deleteTag(hkCityAdv);
		insertTag(hkCityAdv);
		deleteCategory(hkCityAdv);
		insertCategory(hkCityAdv);
		if(hkCityAdv.getState().equals("1")) {
			insertAllAdv(hkCityAdv);
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(HkCityAdv hkCityAdv) {
		super.delete(hkCityAdv);
	}


	/**
	 * 删除标签
	 * @param hkCityAdv
	 * @return
	 */
	private void deleteCategory(HkCityAdv hkCityAdv){
		super.dao.deleteCategory(hkCityAdv);
	}
	/**
	 * 增加标签
	 * @param hkCityAdv
	 * @return
	 */
	private void insertCategory(HkCityAdv hkCityAdv){
		super.dao.insertCategory(hkCityAdv);
	}
	//同步数据
	private void insertAllAdv(HkCityAdv hkCityAdv){
		String sourceId=hkCityAdv.getId();
		HkAllAdv hkAllAdvOld =hkAllAdvService.getBySourceId(sourceId);
		if(null==hkAllAdvOld){
			HkAllAdv hkAllAdv = new HkAllAdv();
			BeanUtils.copyProperties(hkCityAdv, hkAllAdv);
			hkAllAdv.setId(null);
			hkAllAdv.setSourceId(hkCityAdv.getId());
			//类型  1自媒体 2企业广告3城市广告4传统文化
			hkAllAdv.setType("3");
			hkAllAdvService.save(hkAllAdv);
		}
		else{
			HkAllAdv hkAllAdv =hkAllAdvOld;
			BeanUtils.copyProperties(hkCityAdv, hkAllAdv);
			hkAllAdv.setId(hkAllAdvOld.getId());
			hkAllAdvService.save(hkAllAdv);
		}

		hkAllAdvService.deleteTag(hkCityAdv.getId(),hkCityAdv.getTag());
		hkAllAdvService.insertTag(hkCityAdv.getId(),hkCityAdv.getTag());
		hkAllAdvService.deleteCategory(hkCityAdv.getId(),hkCityAdv.getCategory());
		hkAllAdvService.insertCategory(hkCityAdv.getId(),hkCityAdv.getCategory());
	}
	/**
	 * 删除标签
	 * @param hkCityAdv
	 * @return
	 */
	private void deleteTag(HkCityAdv hkCityAdv){
		super.dao.deleteTag(hkCityAdv);
	}
	/**
	 * 增加标签
	 * @param hkCityAdv
	 * @return
	 */
	private void insertTag(HkCityAdv hkCityAdv){
		super.dao.insertTag(hkCityAdv);
	}
}